package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.exception.UpdationNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */

@Service
public class SetmealServiceImpl  implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    DishMapper dishMapper;

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        setmealMapper.add(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for(SetmealDish setmealDish:setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.addBatch(setmealDishes);
        return;
    }

    @Override
    public PageResult<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealsPage = (Page) setmealMapper.pageQuery(setmealPageQueryDTO);
        List<SetmealVO> setmealVOList = setmealsPage.getResult();
        long total = setmealsPage.getTotal();
        return new PageResult<>(total,setmealVOList);
    }

    @Override
    @Transactional
    public void startOrStop(Integer status, long id) {
        if(status.equals(StatusConstant.ENABLE)){
            List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);
            for(SetmealDish setmealDish: setmealDishList){
                //表示有未起售菜品
                if(dishMapper.getById(setmealDish.getDishId()).getStatus().equals(StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        setmealMapper.StartOrStop(id,status);
        return;
    }

    @Override
    @Transactional
    public void DeleteAll(List<Long> ids) {
        //判断套餐是否是起售  ，是则无法删除
        for(Long id : ids){
            if (setmealMapper.getBYId(id).getStatus().equals(StatusConstant.ENABLE)){
                throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.DeleteAll(ids);
        setmealDishMapper.DeleteAll(ids);
        return;
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getById(id);
        return setmealVO;
    }

    @Transactional
    @Override
    public void Update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        Long  id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmeal.setStatus(StatusConstant.ENABLE);

        //先删除原套餐内的菜品关系
        setmealDishMapper.DeleteBySetmealId(id);
        //进行修改重新设置套餐并且重新建立菜品套餐关系
        setmealMapper.Update(setmeal);
        //菜品未设置口味, 则不重新设置菜品口味
        if(setmealDishes ==null || setmealDishes.size() ==0)
        {
            return;
        }
        for (SetmealDish setmealDish: setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        //重新设置菜品套餐关系
        setmealDishMapper.addBatch(setmealDishes);
        return;
    }


    @Override
    public List<Setmeal>  getByCategoryId(Long categoryId){
        List<Setmeal> setmealList = setmealMapper.getByCategoryId(categoryId);
        return setmealList;
    }

    @Override
    public List<DishItemVO> getDishsBySetmealId(Long SetmealId) {
        List<DishItemVO> dishsBySetmealId = setmealDishMapper.getDishsBySetmealId(SetmealId);
        return dishsBySetmealId;
    }

}



